using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using digit.Core;

namespace SMS.UserManager
{
    public class MembersData
    {
        #region CONSTRUCTORS
        public MembersData(string connectionString)
        {
            this._connectionString = connectionString;
        }
        #endregion

        #region ATTRIBUTES
        const string SQL_INSERT = "vn1102_Insert_Members";
        const string SQL_UPDATE = "vn1102_Update_Members";
        const string SQL_DELETE = "vn1102_Delete_Members";
        const string SQL_SELECTONE = "vn1102_Select_Members";
        const string SQL_LISTALL = "Member_ListPaging";
        const string SQL_SEARCH = "vn1102_Members_Search";
        const string vn1102_INSERT_TRANSACTION = "vn1102_Insert_Transaction";
        const string SQL_CHECKOUT = "vn1102_CheckedOut";
        const string SQL_LOGIN = "vn1102_Login";
        const string SQL_VERIFY = "vn1102_Member_Verify";
        const string SQL_CHANGEPASS = "vn1102_ChancePass_Members";
        const string SQL_FRIENDLIST = "vn1102_List_MemberFriends";
        const string SQL_INSERT_FRIEND = "vn1102_Insert_MemberFriends";
        const string SQL_DELETE_FRIEND = "vn1102_Delete_MemberFriends";
        const string SQL_SELECT_ALL = "vn1102_SelectAll_Members";
        private string _connectionString;
        #endregion

        #region METHODS

        public DataTable SelectTopNewMember(object[] values)
        {
            return DataAccess.FillTable(this._connectionString, "vn1102_Select_Top_NewMember", values, true);
        }

        public bool CheckEmail(string strEmail)
        {
            DataTable dt = DataAccess.FillTable(this._connectionString, "vn1102_Members_CheckEmail", strEmail, true);
            return (dt != null && dt.Rows.Count > 0);
        }

        public bool ResetPass(object[] values)
        {
            int i = (int)DataAccess.ExecuteStoreProcedure(this._connectionString, "vn1102_Members_ResetPass", values);
            return i > 0;
        }

        public DataTable Login(object[] values)
        {
            return DataAccess.FillTable(this._connectionString, SQL_LOGIN, values, true);
        }

        public bool Insert(object[] values)
        {
            int i = DataAccess.ExecuteStoreProcedure(this._connectionString, SQL_INSERT, values);
            return i > 0;
        }

        public bool Update(object[] values)
        {
            int i = DataAccess.ExecuteStoreProcedure(this._connectionString, SQL_UPDATE, values);
            return i > 0;
        }

        public bool ChangePass(object[] values)
        {
            int i = DataAccess.ExecuteStoreProcedure(this._connectionString, SQL_CHANGEPASS, values);
            return i > 0;
        }

        public bool Delete(object[] values)
        {
            int i = DataAccess.ExecuteStoreProcedure(this._connectionString, SQL_DELETE, values);
            return i > 0;
        }

        public DataTable Select(object[] values)
        {
            return DataAccess.FillTable(this._connectionString, SQL_SELECTONE, values, true);
        }

        public DataTable Select(string UserName)
        {
            return DataAccess.FillTable(this._connectionString, "vn1102_Select_Members_ByUserName", UserName, true);
        }

        public DataTable CheckUserNameByMail(string Email)
        {
            return DataAccess.FillTable(this._connectionString, "vn1102_Select_UserName_ByEmail", Email, true);
        }

        public DataSet ListAll(object[] values)
        {
            return DataAccess.FillDataSet(this._connectionString, SQL_LISTALL, values, true);
        }

        public DataTable Search(object[] values)
        {
            return DataAccess.FillTable(this._connectionString, SQL_SEARCH, values, true);
        }

        public DataTable SelectAll()
        {
            return DataAccess.FillTable(this._connectionString, SQL_SELECT_ALL, true);
        }

        public string VerifyCode(object[] values)
        {
            return DataAccess.ExecuteGetParameter(this._connectionString, SQL_VERIFY, "@UserName", values, true).ToString();
        }

        public int GetAccAmount(object[] values)
        {
            try
            {
                object obj = DataAccess.ExecuteGetParameter(_connectionString, "vn1102_GetAccAmount", "@Amount", values, true);
                return Convert.ToInt32(obj);
            }
            catch
            {
                return 0;
            }
        }

        public bool ResetPIN(object[] values)
        {
            try
            {
                int i = DataAccess.ExecuteStoreProcedure(_connectionString, "vn1102_ResetPIN_Member", values);
                return i > 0;
            }
            catch { return false; }
        }

        public DataTable SelectByMobileNo(string UserName)
        {
            return DataAccess.FillTable(this._connectionString, "vn1102_Select_Accounts_ByMobileNo", UserName, true);
        }

        public bool UpdatePIN(object[] values)
        {
            try
            {
                int i = DataAccess.ExecuteStoreProcedure(_connectionString, "vn1102_Update_PIN", values);
                return i > 0;
            }
            catch
            {
                return false;
            }
        }

        public bool VerifyPin(object[] values)
        {
            try
            {
                object obj = DataAccess.ExecuteGetParameter(_connectionString, "vn1102_CheckPIN_Account", "@MemberID", values, true);
                return !("".Equals(Util.ReplaceDBNullTo(obj, "")));
            }
            catch
            {
                return false;
            }
        }

        public int CheckAmount(object[] values)
        {
            return (int)DataAccess.ExecuteGetParameter(this._connectionString, "vn1102_", "@RETURN_VALUE", values, true);
        }

        public bool ResetPassByMobile(object[] values)
        {
            try
            {
                return DataAccess.ExecuteStoreProcedure(_connectionString, "vn1102_resetPasswordByMobile", values) > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion
    }
}